﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;

namespace MAT.Controllers
{
    class ctrManageRoom
    {
        private frmManageRoom _frmManageRoom = null;

        public frmManageRoom FrmManageRoom
        {
            get { return _frmManageRoom; }
            set { _frmManageRoom = value; }
        }
       
        MAT_Context context = MATContext.getContext();
        List<ROOM> dep = null;

        private bool check()
        {
            if (_frmManageRoom.txtRomName.Text == "")
            {
                MessageBox.Show("Name is empty");
                _frmManageRoom.txtRomName.Focus();
                return false;
            }
            else
                return true;
        }

        private string getID()
        {
            dep = context.ROOMs.ToList();
            string a = dep[dep.Count - 1].ROOM_ID.ToString();
            int dem = Convert.ToInt16(a.Substring(a.Length - 3));
            if (dem >= 99)
                a = "" + ++dem;
            else
                if (dem >= 9)
                    a = "0" + ++dem;
                else
                    a = "00" + ++dem;
            return "R" + a;
        }

        public void frmManageROOM_Load()
        {
            try
            {
                _frmManageRoom.cmbRoomName.DataSource = context.ROOMs.ToList();
                _frmManageRoom.cmbRoomName.DisplayMember = "ROOM_NAME";
                _frmManageRoom.cmbRoomName.ValueMember = "ROOM_ID";

                _frmManageRoom.txtRomName.GotFocus += new EventHandler(txtRomName_GotFocus);
                _frmManageRoom.txtRomName.MouseClick += new MouseEventHandler(txtRomName_GotFocus);
            }
            catch { }
        }

        public void btnAdd_Click()
        {
            if (_frmManageRoom.btnAdd.Text == "Add")
            {
                if (check())
                {
                    if (context.ROOMs.FirstOrDefault(ro => ro.ROOM_NAME.ToLower() == _frmManageRoom.txtRomName.Text.ToLower()) == null)
                    {
                        ROOM ROOM = new ROOM();
                        ROOM.ROOM_ID = _frmManageRoom.txtRomID.Text;
                        ROOM.ROOM_NAME = _frmManageRoom.txtRomName.Text;
                        ROOM.CLASSIFIED = Convert.ToInt16(_frmManageRoom.cmbClassified.SelectedItem.ToString());
                        try
                        {
                            context.ROOMs.AddObject(ROOM);
                            context.SaveChanges();
                            MessageBox.Show("SUCCESSFUL!");
                            frmManageROOM_Load();
                        }
                        catch { }
                        _frmManageRoom.btnAdd.Text = "New";
                        _frmManageRoom.btnEdit.Enabled = true;
                        _frmManageRoom.cmbRoomName.Enabled = true;
                        _frmManageRoom.txtRomID.Text = "";
                        _frmManageRoom.txtRomName.Text = "";
                        _frmManageRoom.btnReset.Visible = false;
                        _frmManageRoom.btnClose.Text = "Close";
                    }
                    else
                        MessageBox.Show("Room name is exist!");
                }
                return;
            }
            if (_frmManageRoom.btnAdd.Text == "New")
            {
                _frmManageRoom.btnAdd.Text = "Add";
                _frmManageRoom.btnEdit.Enabled = false;
                _frmManageRoom.cmbRoomName.Enabled = false;
                _frmManageRoom.txtRomID.Text = "";
                _frmManageRoom.txtRomName.Text = "";
                _frmManageRoom.txtRomID.Text = getID();
                _frmManageRoom.btnClose.Text = "Cancel";
                _frmManageRoom.btnReset.Visible = true;
                _frmManageRoom.cmbClassified.SelectedItem = "1";
                return;
            }
        }

        public void btnReset_Click()
        {
            _frmManageRoom.txtRomName.Text = "";
            _frmManageRoom.cmbClassified.SelectedItem = "1";
        }

        public void btnEdit_Click()
        {
            ROOM ROOM = context.ROOMs.FirstOrDefault(dep => dep.ROOM_ID == _frmManageRoom.txtRomID.Text);
            if (ROOM != null)
            {
                if (context.ROOMs.FirstOrDefault(ro => ro.ROOM_NAME.ToLower() == _frmManageRoom.txtRomName.Text.ToLower()) == null)
                {
                    try
                    {
                        ROOM.ROOM_NAME = _frmManageRoom.txtRomName.Text;
                        ROOM.CLASSIFIED = Convert.ToInt16(_frmManageRoom.cmbClassified.SelectedItem.ToString());
                        context.SaveChanges();
                        MessageBox.Show("SUCCESSFUL!");
                        frmManageROOM_Load();
                    }
                    catch { }
                }
                else
                    MessageBox.Show("Room name is exist!");
            }
            else
                MessageBox.Show("Not Found!");
            return;
        }

        public void btnClose_Click()
        {
            if (_frmManageRoom.btnClose.Text == "Close")
            {
                _frmManageRoom.Close();
                MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
            }
            if (_frmManageRoom.btnClose.Text == "Cancel")
            {
                _frmManageRoom.btnClose.Text = "Close";
                _frmManageRoom.btnEdit.Enabled = true;
                _frmManageRoom.cmbRoomName.Enabled = true;
                _frmManageRoom.txtRomID.Text = "";
                _frmManageRoom.txtRomName.Text = "";
                _frmManageRoom.btnReset.Visible = false;
                _frmManageRoom.btnAdd.Text = "New";
                return;
            }
        }

        public void cmbRoomName_SelectedIndexChanged()
        {
            try
            {
                string a = _frmManageRoom.cmbRoomName.SelectedValue.ToString();
                ROOM ROOM = context.ROOMs.FirstOrDefault(dep => dep.ROOM_ID == a);
                if (ROOM != null)
                {
                    _frmManageRoom.txtRomID.Text = ROOM.ROOM_ID;
                    _frmManageRoom.txtRomName.Text = ROOM.ROOM_NAME;
                    _frmManageRoom.cmbClassified.SelectedItem = ROOM.CLASSIFIED.ToString();
                }
            }
            catch { }
        }

        public void txtRomName_KeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnAdd_Click();
        }

        void txtRomName_GotFocus(object sender, EventArgs e)
        {
            _frmManageRoom.txtRomName.SelectAll();
        }

    }
}
